function plsb=rpls2()
    plsb=floor(rand(gsize,H)*3)-1; //plan for just H steps
    plsb(1,:)=floor(rand(1,H)*2);
    plsb(gsize,:)=floor(rand(1,H)*2)-1;
endfunction
function plsi=plsir2(pls,w,t)
    plsi=pls;
    eq=1;
    ns=0;
    while eq>0 & ns<4*H
    //for ns=1:4*H//100
        //printf("iter %d\n",eq);
        //calc v
        //TODO size changes
        ns=ns+1;
        A=zeros(H*gsize,(H+1)*gsize);
        B=zeros(H*gsize,1);
        for i=1:gsize
            for j=1:H
                A((i-1)*(H)+j,(i-1)*(H+1)+j)=1;
                a=pls(i,j);
                s=i+a;
                A((i-1)*(H)+j,(s-1)*(H+1)+j+1)=-gm;
                B((i-1)*(H)+j,1)=-rw(i-1,t+j-1,a,w);
            end
        end
        x0=linsolve(A,B);
        //calc new plsi
        for i=1:gsize
            for j=1:H
                plsi(i,j)=getact2(i,j,w,x0,t);
            end
        end
        //st1=genst(plsi);
        //st2=genst(pls);
        eq=sum(~(pls==plsi));
        pls=plsi
    end
endfunction
function a=getact2(s,t,w,x0,st)
    if s==1 then
        r=zeros(2,1);
        r(1)=rw(s-1,st+t-1,0,w)+gm*x0((s-1)*(H+1)+t+1);//TODO t thingy
        r(2)=rw(s-1,st+t-1,1,w)+gm*x0(s*(H+1)+t+1);
        [mr,a]=max(r);
        a=a-1;
    elseif s==gsize
        r=zeros(2,1);
        r(1)=rw(s-1,st+t-1,-1,w)+gm*x0((s-2)*(H+1)+t+1);
        r(2)=rw(s-1,st+t-1,0,w)+gm*x0((s-1)*(H+1)+t+1);
        [mr,a]=max(r);
        a=a-2;
    else
        r=zeros(3,1);
        r(1)=rw(s-1,st+t-1,-1,w)+gm*x0((s-2)*(H+1)+t+1);
        r(2)=rw(s-1,st+t-1,0,w)+gm*x0((s-1)*(H+1)+t+1);
        r(3)=rw(s-1,st+t-1,1,w)+gm*x0(s*(H+1)+t+1);
        [mr,a]=max(r);
        a=a-2;
    end
    //determine best action
endfunction
